
(stream-length (stream 1 2 3))		; => 3

(define (s v) (stream->list v 10))

(s (stream-- (stream 1 2 3)))		; => (-1 -2 -3)

(s (stream-+ (stream 1 2 3)
	     (stream 6 5 4)
	     (stream 1 0 1)))		; => (8 7 8)

stream-null                                 ; => <stream>
(stream? stream-null)			    ; => #t

(define (s v)
  (stream->list v 10))

(s (stream-cons 'a (stream 'b 'c 'd)))	    ; => (a b c d)
(s (stream-cons "a" (stream 'b 'c)))	    ; => ("a" b c)
(stream-cons 'a 3)                          ; => error
(s (stream-cons (stream 'a) (stream 'b)))   ; => (<stream> b)

(stream-null? stream-null)		    ; => #t 
(stream-null? (stream-cons 'a stream-null)) ; => #f
(stream-null? 3)			    ; => #f

(stream-pair? stream-null)		    ; => #f
(stream-pair? (stream-cons 'a stream-null)) ; => #t
(stream-pair? 3)			    ; => #f

(stream-car (stream 'a 'b 'c))              ; => a
(stream-car stream-null)		    ; => error
(stream-car 3)				    ; => error

(s (stream-cdr (stream 'a 'b 'c)))	    ; => (b c)
(stream-cdr stream-null)		    ; => error
(stream-cdr 3)				    ; => error
 
(define from0
  (let loop ((x 0))
    (stream-cons x (loop (+ x 1)))))

(s from0)			            ; => (0 1 2 3 4 5 6 7 8 9)

(s (stream 'a (+ 3 4) 'c))		   ; => (a 7 c)
(stream)                                   ; => <stream-null>

(define (take5 s)
  (stream-unfoldn
   (lambda (x)
     (let ((n (car x)) (s (cdr x)))
       (if (zero? n)
	   (values 'dummy '())
	   (values
	    (cons (- n 1) (stream-cdr s))
	    (list (stream-car s))))))
   (cons 5 s)
   1))

(s (take5 from0))			; => (0 1 2 3 4)

(s (stream-map (lambda (x) (+ x x)) 
	       from0))			; => (0 2 4 6 8 10 12 14 16 18)

(s (stream-map + 
	       (stream 1 2 3) 
	       (stream 4 5 6)))		; => (5 7 9)

(s (stream-map (lambda (x) (expt x x))
	       (stream 1 2 3 4 5)))	; => (1 4 27 256 3125)

(stream-for-each display from0)	        ; => no value, prints 01234 ...

(stream-filter odd? stream-null)           ; => <stream-null>
(s (take5 (stream-filter odd? from0)))	   ; => (stream 1 3 5 7 9)

(let ((s (stream-cons 1 (stream-cons 2 (stream-cons #\newline stream-null)))))
  (stream-for-each display s)
  (stream->list s 3))			; => (1 2 #\newline)

(stream-for-each display* (list->stream (iota 200)))

(require (lib  "40.ss" "srfi"))

(define Q (list->stream '(1 2 3 4 5)))

(define R (list->stream '(a b c d e)))

(define S (stream-map cons Q R))

(s S)			; => ((1 . a) (2 . b) (3 . c) (4 . d) (5 . e))

(define Q_sqrt (stream-map sqrt Q))

(s Q_sqrt)			       ; => (~1 1.414 ~1.732 2 ~2.236)

(s (stream-filter even? Q))		; => (2 4)

(stream-for-each 
 inform 
 (stream-filter even? Q))		; => DISPLAY 2 and 4

(stream-for-each (lambda (a b) 
		     (inform (cons a b))) 
		   Q R)

(stream-finite->list Q)

(call-with-input-file "/etc/passwd"
  (lambda (port)
    (let ((s (port->stream read-char port)))
      (stream-for-each display s))))

;; Series

(stream->list (stream-of-integers 5) 5)	; => (5 6 7 8 9)

(stream-ref (stream-of-integers -1) 564) ; => 563

(stream-ref (stream-of-primes) 12)

(stream->list (stream-of-primes) 128)

(prime? 719)				; => #t

;;

(stream->list (stream-of-primes) 128)

(Pseq '(1 2 3) +inf 0)

